/**
 * All Rights Reserved , Copyright (C) 2023 , 青岛鼎信通讯股份有限公司
 *
 *PmContractApplicantChangeController
 * 项目合同申请人变更
 *
 * 修改纪录
 * 2023-06-11 版本：1.0 wuyuyang 创建。
 * @version 版本：1.0
 * @author 作者：wuyuyang
 * 创建日期：2023-06-11
 */

package com.topscomm.pm.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.topscomm.cbo.service.ICboDeptService;
import com.topscomm.pm.PmSystemConst;
import com.topscomm.pm.pojo.PmContractApplicantChangeEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.pm.controller.auto.PmContractApplicantChangeControllerAuto;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.DateUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.common.TapSystemConst.TapOperationTypeEnum;

/**
 * @author: wuyuyang
 * @date: 2023-06-11
 * @description:项目合同申请人变更Controller
 * @modify:
 */
@RestController
@RequestMapping("pm/contractApplicantChange")
public class PmContractApplicantChangeController extends PmContractApplicantChangeControllerAuto implements IPmContractApplicantChangeController {


	/**
	 * @author: wuyuyang
	 * @date: 2023-06-11
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" enabled=1 ");
		if (paraMap.containsKey("qry_projectid")) {
			String value = paraMap.get("qry_projectid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and projectid ='{0}'", value);
		}
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state={0}", value);
		}
		if (paraMap.containsKey("qry_docno")) {
			String value = paraMap.get("qry_docno").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docno like '%{0}%'", value);
		}
		if (paraMap.containsKey("qry_begindocdate")) {
			String value = paraMap.get("qry_begindocdate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docdate >= {0} ", SQLAdapter.toDateLong(value + " 00:00:00"));
		}
		if (paraMap.containsKey("qry_enddocdate")) {
			String value = paraMap.get("qry_enddocdate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docdate <= {0}", SQLAdapter.toDateLong(value + " 23:59:59"));
		}
		if (paraMap.containsKey("qry_deptname")) {
			String value = paraMap.get("qry_deptname").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (deptid in ( SELECT id from CboDept where fullname like '%{0}%'))", value);
		}
		if (paraMap.containsKey("qry_username")) {
			String value = paraMap.get("qry_username").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (username like '%{0}%')", value);
		}
		if (paraMap.containsKey("qry_deptcode")) {
			String value = paraMap.get("qry_deptcode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and deptcode like '{0}%'", value);
		}
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state={0}", value);
		}
		if (paraMap.containsKey("qry_where")) {
			String value = paraMap.get("qry_where").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and {0}", value);
		}
		return sql.toString();
	}

	/**
	 * @author: wuyuyang
	 * @date: 2023-06-11
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 *
	 * @description: 提交审批前
	 * @param dataMap
	 * @param paraMap
	 * @throws Exception
	 * @author: wuyuyang
	 * @date: 2023-06-11
	 * @modify:
	 */
	@Override
	protected void beforeSubmit(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
		super.beforeSubmit(dataMap, paraMap);
		StringBufferProxy mailtitle = new StringBufferProxy();
		mailtitle.appendSingle("{0}{1}于{2}提交项目合同申请人变更申请,单据编号{3}! ", dataMap.get("dispdeptcode"), dataMap.get("username"), DateUtil.dateToString(new Date(), "yyyy-MM-dd"), dataMap.get("docno"));
		dataMap.put("maintitle", mailtitle.toString());
	}

	/**
	 * @Description:    查询当前单据是否存在交接
	 * @Author:         wuyuyang
	 * @Return:         com.topscomm.pub.vo.ResponseResult<java.lang.Object>
	 * @exception:
	 * @Date:           12/6/2023 下午5:12
	 * @Version:        1.0
	 **/
	@GetMapping(value = "/getContractApplicantChange")
	public ResponseResult<Object> getContractApplicantChange(@RequestParam(required = false) Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" enabled = 1 and projectid = '{0}' order by createon desc",ConvertUtil.convertToString(paraMap.get("id")));
		List<PmContractApplicantChangeEntity> pmContractApplicantChangeEntities = this.service.queryByWhere(sql.toString());
		return ResponseResult.ok(pmContractApplicantChangeEntities);
	}

	/**
	 * @Description:    判断用户是否为cfo
	 * @Author:         wuyuyang
	 * @Return:         com.topscomm.pub.vo.ResponseResult<java.lang.Object>
	 * @exception:
	 * @Date:           12/6/2023 下午10:03
	 * @Version:        1.0
	 **/
	@GetMapping("/isCFO")
	public ResponseResult<Object> isCFO(@RequestParam(required = false) Map<String,Object> paraMap){
		String deptCode = ConvertUtil.convertToString(paraMap.get("deptcode"));
		String usercode = ConvertUtil.convertToString(paraMap.get("usercode"));
		// 获取一级部门
//		String fullname = datacodeCache.getValueDynamic("CboDeptFull", temp, "code", "name");
//		String deptCode = fullname.split("\\\\")[1];
		if (deptCode.contains("_")) {
			deptCode = deptCode.substring(0, deptCode.indexOf("_") + 5);
		} else {
			deptCode = deptCode.substring(0, 4);
		}
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle("select id from misdeptheader where deptcode = '{0}' and category = '{1}' and usercode = '{2}'", deptCode, PmSystemConst.CFOFORCONTRACT, usercode);
		int count = this.service.selectExecute(sql.toString()).size();
		if(count > 0) {
			return ResponseResult.ok(true);
		} else {
			return ResponseResult.ok(false);
		}
	}

	/**
	 * @Description:    查询一级部门id
	 * @Author:         wuyuyang
	 * @Return:         com.topscomm.pub.vo.ResponseResult<java.lang.Object>
	 * @exception:
	 * @Date:           12/6/2023 下午5:12
	 * @Version:        1.0
	 **/
	@GetMapping(value = "/queryDeptcode")
	public ResponseResult<Object> queryDeptcode(@RequestParam(required = false) Map<String,Object> paraMap) {

		try{
			 this.service.queryDeptcode(paraMap);
			 return ResponseResult.ok(true);
		}catch (Exception ex){
			return handleControllerException(ex);
		}

	}

}

